//
//  SuperToursimPlaceVC.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/11/2.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "SuperToursimPlaceVC.h"
#import "SuperToursimPlaceModel.h"
#import "SuperToursimPlaceCell.h"
#import "SuperToursimdetailVC.h"

@interface SuperToursimPlaceVC ()<UITableViewDataSource, UITableViewDelegate,MONActivityIndicatorViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) CGFloat heights;

@property (nonatomic,strong) MONActivityIndicatorView *indicationView;

@end

@implementation SuperToursimPlaceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg 2"]];
    [self addSubViews];
    // 隐藏分割线
    self.tableView.separatorStyle = NO;
    // 加载一个等待动画

    self.indicationView = [[MONActivityIndicatorView alloc] init];
    self.indicationView.center = self.tableView.center;
    CGPoint point = self.indicationView.center;
    point.y = self.tableView.center.y - 200;
    self.indicationView.center = point;
    self.indicationView.delegate = self;
    [self.tableView addSubview:self.indicationView];
    [self.indicationView startAnimating];
    
    [self UpData];
}


// 实现等待图片的加载动画的代理方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    if (index == 0) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 1) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 2) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 3) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    }
    
    return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}

// 加载视图
- (void)addSubViews
{
        // 表的创建
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,  0, KsWidth, KsHeigh ) style:(UITableViewStylePlain)];
    self.tableView.delegate  = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.navigationItem.title = self.tabTitle;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"sfggfesfdff";
    SuperToursimPlaceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SuperToursimPlaceCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    SuperToursimPlaceModel *model = self.dataArray[indexPath.row];
    NSString *stringURL = model.images;
    
    [cell.imageViews sd_setImageWithURL:[NSURL URLWithString:stringURL]];
    cell.titleLabel.text = model.title;
    cell.descLabel.text = model.desc;
    
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect frame = [cell.descLabel.text boundingRectWithSize:CGSizeMake(KsWidth, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGRect newFrame = cell.descLabel.frame;
    newFrame.size.height = frame.size.height;
    cell.descLabel.frame = newFrame;
    self.heights = cell.descLabel.bottom;
    
    [self.indicationView stopAnimating];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.heights;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 传送mtid到下一个页面
    SuperToursimPlaceModel *model = self.dataArray[indexPath.row];
    SuperToursimdetailVC *detailVC = [[SuperToursimdetailVC alloc] init];
    detailVC.IDS = model.ID;
    detailVC.code = model.code;
    detailVC.imageURLs = model.images;
    [self.navigationController pushViewController:detailVC animated:YES];
}



- (void)UpData
{
    
//    NSString *strURL = [NSString stringWithFormat:@"http://www.roadqu.com/api/mobile/qunawan/tour/seasonfunplacelist &a_t=1437534035000&token=55a60422f1df1&mtid=%@&startpos=0&eachpage=5&sign=0050e3a746ed8a3afebffad573ea601ef24eeeea", self.mtids];
//    NSLog(@"%@",strURL);
//    
//    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",strURL]];
//     NSLog(@"%@", url);
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
  //  [self.dataArray removeAllObjects];
    NSURL *url = [NSURL URLWithString:@"http://www.roadqu.com/api/mobile/qunawan/tour/seasonfunplacelist"];
    //创建request网络请求对象
    NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    [mutableURLRequest setHTTPMethod:@"post"];
    //设置请求参数
    NSData *postData = [[NSString stringWithFormat:@"&a_t=1437534035000&token=55a60422f1df1&mtid=%@&startpos=0&eachpage=5&sign=0050e3a746ed8a3afebffad573ea601ef24eeeea",self.mtids]dataUsingEncoding:NSUTF8StringEncoding];
    [mutableURLRequest setHTTPBody:postData];
    
    [NSURLConnection sendAsynchronousRequest:mutableURLRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        // 进行数据解析
        self.dataArray = [NSMutableArray array];

        if (data == nil) {
            return;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"data"];
        
        for (NSDictionary *dic1 in array) {
            // 初始化model
            SuperToursimPlaceModel *model = [[SuperToursimPlaceModel alloc] init];
            model.ID = dic1[@"id"];
            model.code = dic1[@"code"];
            model.title = dic1[@"title"];
            model.images = dic1[@"img"];
            model.desc = dic1[@"desc"];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
       
    }];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
